package top.xzxsrq.fileviewutils.download;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.stereotype.Component;
import top.xzxsrq.utils.PathUtils;
import top.xzxsrq.utils.StringUtilsZX;
import top.xzxsrq.web.download.VideoD;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
@Component
public class VideoDownload extends VideoD {
    private String videoUrl = "videoUrl"; // 视频直链
    private String saveDir = "saveDir"; // 文件保存位置
    private String saveName = "saveName"; // 文件保存名字

    @Override
    public void webRunFunc(String downloadTaskId, String userBasePath, JSONObject jsonObject) throws Exception {
        String saveDir = jsonObject.getString(this.saveDir);
        saveDir = PathUtils.pathResolve(userBasePath, saveDir);
        // 对于保存的名字进行解析，不允许有分割线，有就取最后一个
        String string = jsonObject.getString(this.saveName);
        String[] split = string.split("/|\\\\");
        string = split[split.length - 1];
        if (StringUtilsZX.isBlank(string)) {
            string = "下载";
        }
        VideoDownload videoDownload = new VideoDownload();
        videoDownload.downloadTaskId = downloadTaskId;
        videoDownload.downloadVideo(
                jsonObject.getString(videoUrl),
                saveDir,
                string
        );
    }

    @Override
    public List<Map<Object, Object>> webRunFuncParams() {
        List<Map<Object, Object>> result = new LinkedList<>();
        result.add(webRunFuncParam(
                "prop",
                videoUrl,
                "label",
                "视频直链",
                "type",
                "input"
        ));
        result.add(webRunFuncParam(
                "prop",
                saveDir,
                "label",
                "保存到文件夹",
                "type",
                "SelectBasePath"
        ));
        result.add(webRunFuncParam(
                "prop",
                saveName,
                "label",
                "保存的名字",
                "type",
                "input"
        ));
        return result;
    }
}
